#include "LoadScreen.h"
#include "Application.h"
#include "Fonts.h"
#include "GraphicManager.h"
#include <sfml/Graphics.hpp>

LoadScreen::LoadScreen() : m_textsLoaded(false),
							m_modelsLoaded(false),
							m_animsLoaded(false),
							m_shadersLoaded(false)
{
	Fonts::Istance()->Init();

	sf::Font* f = Fonts::Istance()->GetFont("Consola");
	m_text.SetFont(*f);
	m_text.SetString("Loading Textures..");
	m_text.SetPosition(sf::Vector2f(20, 100));

	GraphicManager::Istance()->Init();

}

LoadScreen::~LoadScreen()
{
	GraphicManager::Istance()->Destroy();
}

void LoadScreen::OnIdle(const sf::Input&)
{
	std::string s = "Loading Textures..";
	if(!m_textsLoaded)
	{
		m_textsLoaded = GraphicManager::Istance()->LoadAllTextures();
	}
	else if(!m_modelsLoaded)
	{	
		m_modelsLoaded = GraphicManager::Istance()->LoadAllModels();
	}
	else if(!m_animsLoaded)
	{	
		m_animsLoaded= GraphicManager::Istance()->LoadAllAnims();
	}
	else if(!m_shadersLoaded)
	{	
		m_shadersLoaded= GraphicManager::Istance()->LoadAllShaders();
	}
	else Application::Istance()->SetState("Main");
	
	if(m_textsLoaded) s += "Done!\nLoading Models..";
	if(m_modelsLoaded) s += "Done!\nLoading Anims..";
	if(m_animsLoaded) s += "Done!\nLoading Shaders..";
	if(m_shadersLoaded) s += "Done!\n";

	m_text.SetString(s);
}

void LoadScreen::OnDraw(sf::RenderWindow& render)
{
	render.SaveGLStates();
	render.Draw(m_text);
	render.RestoreGLStates();
}